package cl.parser.internal;

import java.util.ArrayList;
import java.util.List;

import cl.parser.IParseNode;
import cl.parser.external.WhileKeywordNode;

public class WhileNode extends InternalNode implements IFat {

    @Override
    public String prettyPrint(String indent) {
        String res = "while" + children.get(0).prettyPrint(indent);
        
        if(children.get(1) instanceof CodeBlockNode) {
            res += " " + children.get(1).prettyPrint(indent);
        } else {
            res += "\n" + children.get(1).prettyPrint(indent + "    ");
        }
        
        return res;
    }
    
    @Override
    protected List<IParseNode> getAllowed(IParseNode last, List<IParseNode> soFar) {
        List<IParseNode> allowed = new ArrayList<IParseNode>();
        
        if(last == null) {
            allowed.add(new WhileKeywordNode());
        } else if(last instanceof WhileKeywordNode) {
            allowed.add(new ConditionNode());
        } else if(last instanceof ConditionNode) {
            allowed.add(new CodeBlockNode());
            allowed.add(new StatementNode());
        }
        
        return allowed;
    }

    @Override
    protected boolean isValid(IParseNode last, List<IParseNode> soFar) {
        return last instanceof CodeBlockNode ||
               last instanceof StatementNode;
    }

    @Override
    public void loseWeight() {
        // remove while
        children.remove(0);
    }

}
